#include "lh_show.h"
#include "lh_font.h"

LH_U8 TFT_PutBack = TFT_PUTBACK_NO;



void TFT_Line(LH_U16 x0, LH_U16 y0, LH_U16 x1, LH_U16 y1,LH_U16 color)
{
    RA8875_DrawLine(x0, y0, x1, y1, color);
}



void TFT_FillCircle(LH_U16 x0, LH_U16 y0, LH_U16 r, LH_U16 color)
{
    vs32 i;
    int imax = ((vs32)((vs32)r*707))/1000+1;
    vs32 sqmax = (vs32)r*(vs32)r+(vs32)r/2;
    vs32 x=r;
    TFT_Line(x0-r,y0,x0+r,y0,color);
    for (i=1; i<= imax; i++) 
    {
        if ((i*i+x*x) >sqmax) 
        {
            
            if (x>imax) 
            {           
                TFT_Line(x0-i+1,y0+x, x0+i-1,y0+x,color);
                TFT_Line(x0-i+1,y0-x, x0+i-1,y0-x,color);
            }
            x--;
        }
        
        TFT_Line(x0-x,y0+i, x0+x, y0+i, color);
        TFT_Line(x0-x,y0-i, x0+x, y0-i, color);
    }   
}



void TFT_Circle(LH_U16 x0, LH_U16 y0, LH_U16 r, LH_U16 color)
{
    vs32 i= 0;
    int imax = ((vs32)((vs32)r*707))/1000+1;
    vs32 sqmax = (vs32)r*(vs32)r+(vs32)r/2;
    vs32 y = r;
    
    TFT_SetPoint(x0+i,y0+y, color);
    TFT_SetPoint(x0-i,y0+y, color);
    TFT_SetPoint(x0+y,y0+i, color);
    TFT_SetPoint(x0+y,y0-i, color);
    if (y) 
    {
        TFT_SetPoint(x0+i,y0-y, color);
        TFT_SetPoint(x0-i,y0-y, color);
        TFT_SetPoint(x0-y,y0+i, color);
        TFT_SetPoint(x0-y,y0-i, color);
    }
    
    for (i=1; i<= imax; i++) 
    {
        if ((i*i+y*y) >sqmax)
        {
            TFT_SetPoint(x0+i,y0+y, color);
            TFT_SetPoint(x0-i,y0+y, color);
            TFT_SetPoint(x0+y,y0+i, color);
            TFT_SetPoint(x0+y,y0-i, color);
            if (y) 
            {
                TFT_SetPoint(x0+i,y0-y, color);
                TFT_SetPoint(x0-i,y0-y, color);
                TFT_SetPoint(x0-y,y0+i, color);
                TFT_SetPoint(x0-y,y0-i, color);
            }
            y--;
        }
        TFT_SetPoint(x0+i,y0+y, color);
        TFT_SetPoint(x0-i,y0+y, color);
        TFT_SetPoint(x0+y,y0+i, color);
        TFT_SetPoint(x0+y,y0-i, color);
        if (y) 
        {
            TFT_SetPoint(x0+i,y0-y, color);
            TFT_SetPoint(x0-i,y0-y, color);
            TFT_SetPoint(x0-y,y0+i, color);
            TFT_SetPoint(x0-y,y0-i, color);
        }
    }    
}



void TFT_PutChar(LH_U16 x,LH_U16 y, LH_S8 c, LH_U16 charColor)
{
    LH_U16 i = 0;
    LH_U16 j = 0;
    LH_U8 tmp_char = 0;
    
    for (i = 0; i < 14; i++)
    {
        tmp_char = ascii_8x16[((c-0x20)*16)+i];
        for (j = 0; j < 8; j++)
            if (((tmp_char >> (7-j)) & 0x01) == 0x01)
                TFT_SetPoint(x+j, y+i, charColor);
            else
                if(CheckTftPutBack())
                    TFT_SetPoint(x+j, y+i, GetBackColor());
    }
}



void TFT_String(LH_U16 x, LH_U16 y, LH_S8 *str, LH_U16 Color)
{
  LH_U8 i;
  
  for (i=0; *str != 0; i++)
  {
    TFT_PutChar((x+8*i), y, *str++,Color);
  }
}



void TFT_Bmp(LH_U16 sx, LH_U16 sy, LH_U16 length, LH_U16 width, LH_CU8 *bitbmp)
{
    LH_U16 i1,i2;
    LH_U32 bitnum = 0;
    LH_U16 ex = sx + length;
    LH_U16 ey = sy + width;
    


    TFT_SetWindows(sx, sy, ex-1, ey-1); 
    RA8875_SetCursor(ex-1, sy);         
    RA8875_REG = 0x02; 		            
    
    LCD_OperateMut = 1; 
    for(i1 = sy; i1 < ey; i1 ++)
        for(i2 = sx; i2 < ex; i2 ++)
        {
            RA8875_RAM = bitbmp[bitnum] + (bitbmp[bitnum + 1] << 8); 
            bitnum +=2;
        }
    LCD_OperateMut = 0; 
    TFT_SetWindows(0, 0, 479, 271); 


}



void TFT_Dec(LH_U16 x, LH_U16 y, LH_U32 num, LH_U8 leng, LH_U16 Color)
{
    LH_S8 NumChar1[20] = {0};
    LH_S8 NumChar2[20] = {0};
    LH_S8 Count1 = 0;
    LH_S8 Count2 = 0;
    
    if(num == 0)
    {
        for(; Count2 < leng; Count2 ++)
            NumChar2[Count2] = '0';  
        
        NumChar2[Count2] = 0;
    }
    else
    {
        while(num != 0)
        {
            NumChar1[Count1] = num%10 + '0';
            num /= 10;
            Count1 ++;
        }
        for(; Count1 < leng; Count1 ++)
            NumChar1[Count1] = '0';
        
        for(Count1--; Count1 >= 0; Count1 --)
        {
            NumChar2[Count2] = NumChar1[Count1];
            Count2 ++;
        }
        NumChar2[Count2] = 0;
    }
    
    TFT_String(x, y, NumChar2, Color);
}



void TFT_DecDotp(LH_U16 x, LH_U16 y, LH_U32 num, LH_U8 leng, LH_U8 dotp, LH_U16 Color)
{
    LH_S8 NumChar1[20] = {0};
    LH_S8 NumChar2[20] = {0};
    LH_S8 Count1 = 0;
    LH_S8 Count2 = 0;
    
    if(num == 0)
    {
        for(; Count2 < leng; Count2 ++)
            NumChar2[Count2] = '0';  
        NumChar2[leng-1-dotp] = '.';
        NumChar2[Count2] = 0;
    }
    else
    {
        while(num != 0)
        {
            if(Count1 == dotp)
            {
                NumChar1[Count1] = '.';
                Count1 ++;
            }  
            NumChar1[Count1] = num%10 + '0';
            num /= 10;
            Count1 ++;
        }
        for(; Count1 < leng; Count1 ++)
            NumChar1[Count1] = '0';
        
        for(Count1--; Count1 >= 0; Count1 --)
        {
            NumChar2[Count2] = NumChar1[Count1];
            Count2 ++;
        }
        NumChar2[Count2] = 0;
    }
    
    TFT_String(x, y, NumChar2, Color);
}









